package com.mykt.thread.config;

import java.util.concurrent.*;
import org.springframework.context.annotation.Configuration;

/**
 * 线程池配置
 **/
@Configuration
public class ThreadPoolConfig
{
    // 线程池的核心线程数
    static int corePoolSize = 5;
    // 线程池的最大线程数
    static int maximumPoolSize = 10;
    // 额外的空闲线程的存活时间
    static long keepAliveTime = 5000; // 5 秒
    // 存活时间的单位
    static TimeUnit unit = TimeUnit.MILLISECONDS;
    // 用于保存等待执行的任务的阻塞队列
    static ArrayBlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<>(100);
    // 创建一个线程工厂，通常可以使用默认的线程工厂
    static ThreadFactory threadFactory = Executors.defaultThreadFactory();

    // 在任务无法被执行时的处理策略，这里使用预定义的策略：丢弃最旧的任务
    static RejectedExecutionHandler handler = new ThreadPoolExecutor.DiscardOldestPolicy();

    public static ThreadPoolExecutor executor=null;

   static {
               executor=new ThreadPoolExecutor(
               corePoolSize,
               maximumPoolSize,
               keepAliveTime,
               unit,
               workQueue,
               threadFactory,
               handler);
   }


   public static Future<String> submit(Runnable runnable){
       Future<String> submit = (Future<String>) executor.submit(runnable);
       return submit;
   }



}


